<?php
class LoginApp extends CommonApp {

   	//app登录
    public function LoginAct(){
    	if($_GET){
    		//用户名判断
    		if(empty($_GET['tel']))
    			exit(My::encodeJson(array('status' => '0', 'message'=>'无接收到tel！')));
    		//密码判断
    		if(!My::is_string($_GET['password']))
    			exit(My::encodeJson(array('status' => '0', 'message'=>'密码有误！')));
	        $result = DB('members')->where("tel='{$_GET['tel']}' AND password='".md5(md5($_GET['password']))."'")->cache()->find();
	        if(is_array($result)){
	            if($result)
	                echo My::encodeJson(array('status' => '1', 'message'=>'登录成功！','data'=>array('id'=>$result['id'],'tel'=>$result['tel'])));
	            else
	                echo My::encodeJson(array('status' => '0', 'message'=>'登录失败！'));
	        }else
	            exit(My::encodeJson(array('status' => '0', 'message'=>'用户名或密码错误！')));
    	}
    }

    //app注册
    public function registerAct(){
    	if($_GET){
            if(!isset($_GET['tel']))
                exit(My::encodeJson(array('status' => '0', 'message'=>'无接收到tel！')));
            if(!My::is_string($_GET['password']))
                exit(My::encodeJson(array('status' => '0', 'message'=>'无接收到password！')));
            $data['tel'] = $_GET['tel'];
            $data['password'] = md5(md5($_GET['password']));
            $data['adddate'] = date("Y-m-d H:i:s");
            $ok = DB('members')->where("tel='{$_GET['tel']}'")->cache()->find();
            if($ok)
            	exit(My::encodeJson(array('status' => '2', 'message'=>'该手机号已注册！')));
            $result = DB('members')->clear()->add($data);
            if ($result) echo My::encodeJson(array('status' => '1', 'message'=>'注册成功！'));
            else echo My::encodeJson(array('status' => '0', 'message'=>'注册失败！'));
        }
    }

    //绑定花盆
    public function bindAct(){
    	if($_GET){
    		$data['uid'] = $_GET['uid'];//花盆uid
    		$data['parent_id'] = $_GET['parent_id'];//用户id
    		$data['name'] = $_GET['name'];//花盆昵称
    		if(!isset($data['uid']))
                exit(My::encodeJson(array('status' => '0', 'message'=>'无接收花盆的uid！')));
            if(!isset($data['parent_id']))//花盆uid
                exit(My::encodeJson(array('status' => '0', 'message'=>'无接收用户的parent_id！')));
            $own = DB('datas')->where("uid = '".$data['uid']."'")->getField('parent_id');
            if($own)
            	exit(My::encodeJson(array('status' => '0', 'message'=>'该花盆已被人领走！')));
           	$result = DB('datas')->where("uid = '".$data['uid']."'")->save($data);
           	if($result) echo My::encodeJson(array('status' => '1', 'message'=>'绑定成功！'));
           	else echo My::encodeJson(array('status' => '0', 'message'=>'绑定失败！'));
    	}
    }

    //获取花盆信息，参数用户id、
    public function pot_infoAct(){
    	if($_GET){
    		$parent_id = $_GET['id'];//用户id
    		if(!isset($parent_id))
    			exit(My::encodeJson(array('status' => '0', 'message'=>'无接收到用户id！')));
    		$result = DB('datas')->where("parent_id = '".$parent_id."'")->select();
            foreach($result as &$value){
                $value['params'] = My::decodeJson($value['params']);
            }
    		if($result) echo My::encodeJson(array('status' => '1', 'message'=>'查询成功！','data'=>$result));
    		else echo My::encodeJson(array('status' => '0', 'message'=>'暂无花盆！'));

    	}
    }

    //app注册
    public function tel_infoAct(){
        if($_GET){
            if(!isset($_GET['tel']))
                exit(My::encodeJson(array('status' => '0', 'message'=>'无接收到tel！')));
            $data['tel'] = $_GET['tel'];
            $ok = DB('members')->where("tel='{$_GET['tel']}'")->cache()->find();
            if($ok)
                echo My::encodeJson(array('status' => '1', 'message'=>'手机已注册！'));
            else
                echo My::encodeJson(array('status' => '0', 'message'=>'手机未注册！'));
        }
    }

    //获取花盆信息，参数花盆id、
    public function pot_one_infoAct(){
        if($_GET){
            $pot_uid = $_GET['uid'];//花盆uid
            if(!isset($pot_uid))
                exit(My::encodeJson(array('status' => '0', 'message'=>'无接收到花盆id！')));
            $result = DB('datas')->where("uid = '".$pot_uid."'")->find();
            $result['params'] = My::decodeJson($result['params']);
            if($result) echo My::encodeJson(array('status' => '1', 'message'=>'查询成功！','data'=>$result));
            else echo My::encodeJson(array('status' => '0', 'message'=>'暂无花盆！'));
        }
    }
}